angular.module('app')
		.factory('fileReader', [ "$q", "$log", function($q, $log) {
			var onLoad = function(reader, deferred, scope) {
				return function() {
					scope.$apply(function() {
						var data = reader.result;
	                    //加载图片获取图片真实宽度和高度
	                    var image = new Image();
	                    image.onload=function(){
	                        var width = image.width;
	                        var height = image.height;
	                        deferred.resolve(image);
	                    };
	                    image.src= data;
					});
				};
			};

			var onError = function(reader, deferred, scope) {
				return function() {
					scope.$apply(function() {
						deferred.reject(reader.result);
					});
				};
			};

			var getReader = function(deferred, scope) {
				var reader = new FileReader();
				reader.onload = onLoad(reader, deferred, scope);
				reader.onerror = onError(reader, deferred, scope);
				return reader;
			};

			var readAsDataURL = function(file, scope) {
				var deferred = $q.defer();
				var reader = getReader(deferred, scope);
                reader.readAsDataURL(file);
                
				return deferred.promise;
			};

			return {
				readAsDataUrl : readAsDataURL
			};
		} ])
		.directive('suiImageLoad', [ '$interval', function($interval) {
			return {
				restrict : 'E',
				templateUrl : 'js/directives/tpl/image-load.html',
				scope : {
					src : '=suiImgSrc',
					alt : '=suiImgAlt',
					progress : '=suiImgProgress'
				},
				replace : true,
				transclude : true,
				link : function(scope, element, attrs) {
				}
			};
		} ])
		.directive('suiMutilImageUpload', [ '$timeout', 'fileReader', function($timeout, fileReader) {
			return {
				restrict : 'EA',
				templateUrl : 'js/directives/tpl/mutil-image-upload.html',
				scope : {
		            rat : '=suiMiuRat',
		            maxWidth : '=suiMiuMaxWidth',
		            minWidth : '=suiMiuMinWidth',
		            maxHeight : '=suiMiuMaxHeight',
		            minHeight : '=suiMiuMinHeight',
		            width : '=suiMiuWidth',
		            height : '=suiMiuHeight',
		        },
		        replace : true,
				transclude : true,
				link : function(scope, element, attrs) {
					var allow = function(w, h) {
						var rat = scope.rat;
						var maxWidth = scope.maxWidth;
						var minWidth = scope.minWidth;
						var maxHeight = scope.maxHeight;
						var minHeight = scope.minHeight;
						if(rat !== w / h) {//比例不对
							return false;
						}
						if(w > maxWidth || w < minWidth || h > maxHeight || h < minHeight) {//尺寸不对
							return false;
						}
						return true;
					}
					
					scope.imgFiles = [];
					var fileInput = angular.element(element).find('input[type="file"]');
					scope.addImage = function() {
						$timeout(function() {
							fileInput.click();
						}, 0);
					}
					scope.fileChange = function(files) {
						var file = files[0];
						if(file) {
							fileReader.readAsDataUrl(file, scope).then(function(result){
								if(allow(result.width, result.height)) {
									scope.imgFiles.push({
										src : result.src,
										progress : 0,
										alt : file.name
									});
								}
							});
						}
					}
				}
			};
		} ]);